﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ThreeThingGame;

namespace ProtectThePie
{
    public class HealthBar
    {
        private float maxHealth;
        private Rectangle rectangle;
        private Color color;
        private Texture2D texture;

        public float Health;

        public HealthBar(Texture2D texture, Point position, Point size, float maxHealth)
        {
            this.texture = texture;
            this.rectangle = new Rectangle(position.X, position.Y, size.X, size.Y);
            this.maxHealth = maxHealth;
            this.Health = maxHealth;
        }

        public HealthBar(Texture2D texture, Vector2 bugPosition, float bugRadius, float maxHealth)
        {
            this.texture = texture;
            this.rectangle = new Rectangle();
            calculatePosition(bugPosition, bugRadius);
            this.maxHealth = maxHealth;
            this.Health = maxHealth;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            float healthShort = Health / maxHealth;

            spriteBatch.Draw(texture, rectangle, Color.Black);

            color = new Color(1 - healthShort, healthShort, 0);

            spriteBatch.Draw(texture, new Rectangle(rectangle.X + 3, rectangle.Y + 3, (int)System.Math.Round((rectangle.Width - 6) * healthShort), rectangle.Height - 6), color);
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 bugPosition, float bugRadius)
        {
            float healthShort = Health / maxHealth;

            color = new Color(1 - healthShort, healthShort, 0);

            calculatePosition(bugPosition, bugRadius);
            spriteBatch.Draw(texture, new Rectangle(rectangle.X, rectangle.Y, (int)(rectangle.Width * healthShort), rectangle.Height), color);

        }

        public void Update(float newHealth)
        {
            if (newHealth < 0)
            {
                Health = 0;
            }
            else
            {
                this.Health = newHealth;
            }
        }

        public void calculatePosition(Vector2 bugPosition, float bugRadius)
        {
            this.rectangle.X = (int)(bugPosition.X - bugRadius);
            this.rectangle.Y = (int)(bugPosition.Y - bugRadius) - 20;
            this.rectangle.Width = (int)(bugRadius * 2);
            this.rectangle.Height = 10;
        }
    }
}
